package com.luoxc.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.luoxc.pojo.Emp;
import com.luoxc.pojo.PageBean;
import com.luoxc.pojo.Result;
import com.luoxc.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    /*
    * page = 1
    * pageSize = 10
    * gender
    * start
    * end
    * 分页查询员工信息,并且进行条件筛选
    * */
    @GetMapping
    private Result pageBean(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                            String name, Short gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        PageBean pageBean = empService.page(page,pageSize,name,gender,start,end);
        return Result.success(pageBean);
    }
    /*
    * 1,2,3
    * 批量删除员工信息
    * */
    @DeleteMapping("/{ids}")
    private Result deletes(@PathVariable List<Integer> ids){
        empService.deletes(ids);
        return Result.success();
    }
    /*
    * Emp
    * 新增员工信息
    * */
    @PostMapping
    private Result add(@RequestBody Emp emp){
        empService.add(emp);
        log.info("新增:" + emp);
        return Result.success();
    }
    /*
    * 根据id进行查询
    * */
    @GetMapping("/{id}")
    private Result queryId(@PathVariable Integer id){
        Emp emp = empService.queryId(id);
        return Result.success(emp);
    }
    /*
    * 根据id修改员工
    * */
    @PutMapping
    private Result update(@RequestBody Emp emp){
        log.info("编辑人员:" + emp);
        empService.update(emp);
        return Result.success();
    }
}
